package com.javashow.main.menu.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javashow.main.menu.dao.IMainMenuDao;
import com.javashow.main.menu.service.IMainMenuService;
import com.javashow.main.menu.util.MainMenuConstants;
import com.javashow.pojo.MainMenu;

@Service(value = "mainMenuService")
public class MainMenuServiceImpl implements IMainMenuService, MainMenuConstants{

	@Autowired
	private IMainMenuDao mainMenuDao;
	
	@Override
	/**
	 * 将主菜单对象list 转化成json格式传到controller层
	 */
	public JSONObject queryMainMenu() {
		JSONObject result = new JSONObject();
		JSONArray array = new JSONArray();
		
		List<MainMenu> mainMenus = mainMenuDao.queryMainMenu();
		
		if(mainMenus != null && mainMenus.size() >0) {
			
			for(MainMenu mm : mainMenus) {
				JSONObject tmp = new JSONObject();
				tmp.accumulate("id", mm.getId());
				tmp.accumulate("code", mm.getCode());
				tmp.accumulate("text", mm.getName());
				tmp.accumulate("url", mm.getUrl());
				tmp.accumulate("order_num", mm.getOrder_num());
				tmp.accumulate("leaf", IS_LEAF == mm.getIs_leaf() ? true : false);
				tmp.accumulate("parent_id", mm.getParent_id());
				
				array.add(tmp);
			}
		}
		result.accumulate("totalProperty", mainMenus.size());
		result.accumulate("rows", array);
		return result;
	}
	
}









